package org.phoenixframework.api.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.phoenixframework.api.rbac.mapper.SysDeptMapper;
import org.phoenixframework.api.rbac.service.ISysDeptService;
import org.phoenixframework.common.entity.SysDept;
import org.phoenixframework.common.entity.SysDept;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统部门表 服务实现类
 * </p>
 *
 * @author doublelife
 * @since 2021-05-08
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    private void findChildrenIdByParentId(Long parentId, List<Long> ids) {
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDept::getParentId, parentId);
        List<SysDept> sysDepts = this.baseMapper.selectList(wrapper);
        sysDepts.forEach(item -> {
            ids.add(item.getId());
            this.findChildrenIdByParentId(item.getId(), ids);
        });

    }

    @Override
    public boolean removeChildrenById(Long id) {
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(id);
        this.findChildrenIdByParentId(id, ids);
        return this.baseMapper.deleteBatchIds(ids) > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public List<Long> findChildrenIds(Long deptId) {
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(deptId);
        this.findChildrenIdByParentId(deptId, ids);
        return ids;
    }
}
